package com.wx.uid.config;

import com.wx.uid.UidGenerator;
import com.wx.uid.impl.CachedUidGenerator;
import com.wx.uid.worker.DisposableWorkerIdAssigner;
import com.wx.uid.worker.WorkerIdAssigner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>Description: uid生成配置</p>
 * <p>Copyright: Copyright (c)2020</p>
 * <p>Company: tope</p>
 * <P>Created Date :2020年04月23日</P>
 * @version 1.0
 */
@Configuration
public class UidGeneratorConfig {

    @Value("${timeBits}")
    private int timeBits;
    @Value("${workerBits}")
    private int workerBits;
    @Value("${seqBits}")
    private int seqBits;
    @Value("${epochDate}")
    private String epochDate;
    @Value("${boostPower}")
    private int boostPower;
    @Value("${paddingFactor}")
    private int paddingFactor;

    /**
     * 用完即弃的 WorkerIdAssigner，依赖 DB 操作
     * 这里可以做定制，可以依赖 Redis
     */
    @Bean
    public WorkerIdAssigner workerIdAssigner() {
        return new DisposableWorkerIdAssigner();
    }

    @Bean
    public UidGenerator uidGenerator() {
        CachedUidGenerator uidGenerator = new CachedUidGenerator();
        uidGenerator.setWorkerIdAssigner(workerIdAssigner());
        uidGenerator.setTimeBits(timeBits);
        uidGenerator.setWorkerBits(workerBits);
        uidGenerator.setSeqBits(seqBits);
        uidGenerator.setEpochStr(epochDate);
        uidGenerator.setBoostPower(boostPower);
        uidGenerator.setPaddingFactor(paddingFactor);
        // 还有一些可选配置，此处使用默认值
        return uidGenerator;
    }
}
